using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Gencode.PrsCommon.App;

namespace Personalplaner.Modules.Personoverview
{
    [ToolboxItem(false)]
    public partial class JahresplanView : UserControl, IPersonDetail
    {
        List<IPersonRow> row = null;
        DataTable dt = null;
        int prsID;
        int yearID;

        public JahresplanView(int prsid)
        {
            InitializeComponent();

            Alles = true;

            this.prsID = prsid;
            yearID = Verwaltung.YearID;
            SetData();
        }

        public void SetData()
        {
			dt = new DataTable("1");

            DJPGenerator jpGenerator = new DJPGenerator(prsID, yearID, DienstIDs, MitWochenend, MitFeiertagen, Alles);
            row = jpGenerator.Row;

            jahrGrid = GPersonGridGenerator.CreateGrid(jahrGrid, dt, GPersonGridGenerator.GRIDTYPE.Jahresplan, row);

			if (GPerson.ShowSaldo == 1)
				addSaldo();

			for (int i = 0; i < jahrGrid.Columns.Count; i++)
			{
				if (i < 1)
					jahrGrid.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
				else
					jahrGrid.Columns[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
			}

			SetGridSize();
        }

        private void addSaldo()
        {
            dt.Columns.Add("Soll");
            dt.Columns.Add("Anp.");
            dt.Columns.Add("Diff");

            jahrGrid.Columns[jahrGrid.ColumnCount -1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            jahrGrid.Columns[jahrGrid.ColumnCount - 2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            jahrGrid.Columns[jahrGrid.ColumnCount - 3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            jahrGrid.Columns[jahrGrid.ColumnCount - 1].SortMode = DataGridViewColumnSortMode.NotSortable;
            jahrGrid.Columns[jahrGrid.ColumnCount - 2].SortMode = DataGridViewColumnSortMode.NotSortable;
            jahrGrid.Columns[jahrGrid.ColumnCount - 3].SortMode = DataGridViewColumnSortMode.NotSortable;

            PSaldoColumn saldo = null;
            int c = jahrGrid.ColumnCount;
            int columnCount;
            for (int r = 0; r < row.Count; r++)
            {
                columnCount = row[r].Column.Count - 1;
                if (row[r].Column[columnCount] is PSaldoColumn)
                {
                    saldo = (PSaldoColumn)row[r].Column[columnCount];
                    jahrGrid[c - 4, r].Value = Util.RoundDouble(saldo.Haben + saldo.Ferien, 2);
                    jahrGrid[c - 3, r].Value = Util.RoundDouble(saldo.Soll, 2);
                    jahrGrid[c - 2, r].Value = Util.RoundDouble(saldo.Anpassung, 2);
                    jahrGrid[c - 1, r].Value = Util.RoundDouble((saldo.Haben + saldo.Ferien) - saldo.Soll + saldo.Anpassung, 2);
                }
            }
        }

        public void SetCellColor()
        {
            GGridColor.SetCellColor(jahrGrid, row);
        }

        private void SetGridSize()
        {
            if (jahrGrid.Rows.Count > 0)
            {
                //Height
				jahrGrid.Height=((jahrGrid.RowCount * jahrGrid.Rows[0].Height) + jahrGrid.ColumnHeadersHeight) + 3;
				
				int width = 0;
				int i = 0;
				while (i < jahrGrid.ColumnCount)
				{
					width += jahrGrid.Columns[i].Width;
					i++;
				}

				jahrGrid.Width = width + 3;
            }
        }

        #region Properties
        internal List<IPersonRow> Row
        {
            get { return row; }
            set { row = value; }
        }

        #endregion

        private void CPersonDetailJahresplan_Load(object sender, EventArgs e)
        {
            SetCellColor();
			SetGridSize();
        }

        private void CPersonDetailJahresplan_SizeChanged(object sender, EventArgs e)
        {
            SetGridSize();
        }

        public DataGridView Grid
        {
            get { return jahrGrid; }
        }

		public string Title
		{
			get { return "Jahresplan"; }
		}

        public string DienstIDs
        {
            private get;
            set;
        }

        public bool MitFeiertagen
        {
            private get;
            set;
        }

        public bool MitWochenend
        {
            private get;
            set;
        }

        public bool Alles
        {
            private get;
            set;
        }

		#region IPersonDetail Members

		/// <summary>
		/// Resets grid with data from the given year
		/// </summary>
		/// <param name="year">YearID</param>
		public void ResetGrid(int year)
		{
            if (year > 0)
                yearID = year;

			SetData();
			SetCellColor();
			SetGridSize();
		}

		#endregion
	}
}
